# Contributor: Natanael Copa <ncopa@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua-ldap
_pkgname=lualdap
pkgver=1.3.1
pkgrel=1
pkgdesc="Lua binding to LDAP"
url="https://github.com/lualdap/lualdap"
arch="all"
license="MIT"
options="!check" # fails to detect correct builddir
makedepends="openldap-dev"
checkdepends="openldap openldap-back-mdb lua-busted"
source="$_pkgname-$pkgver.tar.gz::https://github.com/lualdap/lualdap/archive/v$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_package"
done

prepare() {
	default_prepare

	sed -i -e '/LUA_VERSION_NUM/d' config

	local lver; for lver in $_luaversions; do
		cp -r "$builddir" "$builddir-$lver"
	done
}

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		cd "$builddir-$lver"
		make CFLAGS="$CFLAGS -fPIC -ansi -Wall $(pkg-config --cflags lua$lver)" \
			OPENLDAP_LIB="-lldap -llber"
	done
}

check() {
	local lver; for lver in $_luaversions; do
		msg "Testing with Lua $lver..."

		cd "$builddir-$lver"
		make LUA_CPATH="src/?.so.$V" LUA=lua$lver check
	done
}

package() {
	mkdir -p "$pkgdir"
}

_package() {
	local lver="${subpkgname:3:3}"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$_pkgname/$pkgver-1"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	replaces="lua$lver-lualdap"  # for backward compatibility (v3.5)
	[ "$lver" = "5.1" ] && replaces="${replaces:-} $pkgname"  # for backward compatibility (<v3.4)

	mkdir -p "$subpkgdir/usr/lib/lua/$lver"
	cd "$builddir-$lver"
	make DESTDIR="$subpkgdir" \
		LUA=lua$lver \
		LUA_LIBDIR="$(pkg-config --variable=INSTALL_CMOD lua$lver)" \
		install

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="
29d622ecf63214ac26ade71478c383e4f1b7468097bf19b99b8209b3f6cbb2078298a149d64ba8a7ca4abe97aa45550982792fa4ac0a8014f7daf709fe725e9f  lualdap-1.3.1.tar.gz
"
